package com.miguo.receivers;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.miguo.activity.SignActivity;
import com.miguo.app.AppContext;
import com.miguo.ui.R;
import com.miguo.utils.DateUtils;

public class SignAlarmReceiver extends BroadcastReceiver {

    public static final String SIGN_ALARM_ACTION = "com.miguo.ui.sign_alarm";

    private Context mContext;

    private void sendNotifiction() {
//        NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
//        Notification mNotification = new Notification();
//        mNotification.icon = R.drawable.noti_icon;
//        mNotification.defaults = Notification.DEFAULT_ALL;
//        mNotification.tickerText = "亲，今天还没有签到呦";
//        mNotification.when = System.currentTimeMillis();
//        mNotification.flags = Notification.FLAG_AUTO_CANCEL;
//        int requestCode = (int) System.currentTimeMillis();
//        Intent intent = new Intent(mContext, SignActivity.class);
//        intent.putExtra("sign", true);
//        PendingIntent contentIntent = PendingIntent.getActivity(mContext, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//        mNotification.setLatestEventInfo(mContext, "签到提醒", "亲，今天还没有签到呦", contentIntent);
//        mNotificationManager.notify(requestCode, mNotification);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
        mBuilder.setAutoCancel(true)//点击后让通知将消失
                .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setDefaults(Notification.DEFAULT_ALL)
                .setOngoing(false)
                .setSmallIcon(R.drawable.noti_icon)
                .setContentTitle("签到提醒")
                .setContentText("亲，今天还没有签到呦")
                .setTicker("亲，今天还没有签到呦");
        Intent intent = new Intent(mContext, SignActivity.class);
        intent.putExtra("sign", true);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        int requestCode = (int) System.currentTimeMillis();
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(requestCode, mBuilder.build());
    }

    public void onReceive(Context context, Intent intent) {
        mContext = context;
        if ("com.miguo.ui.sign_alarm".equals(intent.getAction())) {
            String signTime = AppContext.Prefs().getSignTime();
            String signAlarmTime = AppContext.Prefs().getSignAlarmTime();
            boolean signSwitch = AppContext.Prefs().getSignAlramSwitch();
            if (DateUtils.isCurrentDay(signTime) == false && DateUtils.isCurrentDay(signAlarmTime) == false && signSwitch) {
                AppContext.Prefs().setSignAlarmTime(String.valueOf(System.currentTimeMillis()));
                sendNotifiction();
            }
        }
    }
}